//
//  AIGirlSubscribeAlreadyViewController.swift
//  AIGirl
//
//  Created by Rae on 2024/1/3.
//

import UIKit

class AIGirlSubscribeAlreadyViewController: AIGirlBaseViewController {

    private lazy var bgImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Subscribe_bg").mode(.scaleAspectFill).end()
    private lazy var shadowView = c_make_ex(UIView.self).end()
    private lazy var bottomView = c_make_ex(UIView.self).bgColor("#120D00").end()
    private lazy var backButton = c_make_ex(UIButton.self)
        .image("AIGirl_Subscribe_back").end()
    private lazy var titleView = c_make_ex(UIImageView.self)
        .image("AIGirl_Subscribe_titleView").mode(.scaleAspectFill).end()
    private lazy var timeLabel = c_make_ex(UILabel.self)
        .color("#FFD36C").font(20.font_semiBold).end()
    private var contentV = AIGirlPremiumCotnentView()
    private lazy var termButton = c_make_ex(UIButton.self)
        .color(.white.alpha(0.5)).font(12.font_regular).end()
    private lazy var policyButton = c_make_ex(UIButton.self)
        .color(.white.alpha(0.5)).font(12.font_regular).end()
    
    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            bgImageView
            shadowView
            bottomView
            backButton
            titleView
            timeLabel
            contentV
            termButton
            policyButton
        }
        
        bgImageView.fillContainer()
        shadowView.leading(0).trailing(0).top(0).height(400)
        bottomView.leading(0).trailing(0).bottom(0)
        bottomView.Top == shadowView.Bottom
        backButton.leading(15).top(.statusHeight + 7.0).size(40)
        
        let bottom = CGFloat.bottomSpace + 100.0
        view.layout {
            |-17-titleView ~ 24
            15
            |-17-timeLabel ~ 25
            25
            |contentV| ~ 185
            bottom
        }
//        titleView.leading(17).height(24).width(170)
//        titleView.Bottom == timeLabel.Top - 15
//        timeLabel.leading(17).trailing(25).height(25)
//        timeLabel.Bottom == contentV.Top - 25
//        contentV.leading(0).trailing(0).height(185)
        
        termButton.trailing(.screenWidth/2 + 25).bottom(25)
        policyButton.leading(.screenWidth/2 + 25).bottom(25)

        termButton.style { v in
            let attributedString = NSAttributedString(string: "Term of Us", attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
            v.setAttributedTitle(attributedString, for: .normal)
            v.addTarget(self, action: #selector(clickTermButton), for: .touchUpInside)
        }
        policyButton.style { v in
            let attributedString = NSAttributedString(string: "Privacy Policy", attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
            v.setAttributedTitle(attributedString, for: .normal)
            v.addTarget(self, action: #selector(clickPolicyButton), for: .touchUpInside)
        }
        backButton.addTarget(self, action: #selector(clickCloseButton), for: .touchUpInside)
        
        let startT = UserDefaultsManager.getString(forKey: .VIPStartTime) ?? ""
        let endT = UserDefaultsManager.getString(forKey: .VIPEndTime) ?? ""
        if startT.count > 0 && endT.count > 0 {
            timeLabel.text = startT + "-" + endT
        }
        
        let bgUrl = UserDefaultsManager.getString(forKey: .girlAvatar)
        bgImageView.loadImage(url: bgUrl)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        shadowView.fj.gradientColors(.topToBottom(["#000000".toColor.alpha(0),"#120D00".toColor]))
    }
}

//MARK: action
extension AIGirlSubscribeAlreadyViewController {
    
    @objc private func clickTermButton(_ sender: UIButton){
        let webVc = AIGirlWebViewController()
        webVc.urlString = AGirlConstants.terms
        webVc.titleName = sender.titleLabel?.text
        self.navigationController?.pushViewController(webVc, animated: true)
    }
    
    @objc private func clickPolicyButton(_ sender: UIButton){
        let webVc = AIGirlWebViewController()
        webVc.urlString = AGirlConstants.policy
        webVc.titleName = sender.titleLabel?.text
        self.navigationController?.pushViewController(webVc, animated: true)
    }
    
    @objc private func clickCloseButton(){
        self.dismiss(animated: true)
    }
}
